package com.codename1.samples;

import static com.codename1.ui.CN.*;
import com.codename1.ui.Display;
import com.codename1.ui.Form;
import com.codename1.ui.Dialog;
import com.codename1.ui.Label;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.io.Log;
import com.codename1.ui.Toolbar;
import java.io.IOException;
import com.codename1.ui.layouts.BoxLayout;
import com.codename1.io.NetworkEvent;
import com.codename1.ui.*;
import com.codename1.ui.layouts.*;
import com.codename1.ui.events.*;

/**
 * This file was generated by <a href="https://www.codenameone.com/">Codename
 * One</a> for the purpose of building native mobile applications using Java.
 */
public class SideMenuTest3086 {

    private Form current;
    private Resources theme;

    public void init(Object context) {
        // use two network threads instead of one
        updateNetworkThreadCount(2);

        theme = UIManager.initFirstTheme("/theme");

        // Enable Toolbar on all Forms by default
        Toolbar.setGlobalToolbar(true);

        // Pro only feature
        Log.bindCrashProtection(true);

        addNetworkErrorListener(err -> {
            // prevent the event from propagating
            err.consume();
            if (err.getError() != null) {
                Log.e(err.getError());
            }
            Log.sendLogAsync();
            Dialog.show("Connection Error", "There was a networking error in the connection to " + err.getConnectionRequest().getUrl(), "OK", null);
        });
    }

    public void start() {
        if (current != null) {
            current.show();
            return;
        }
        Toolbar.setGlobalToolbar(true);
        Toolbar.setOnTopSideMenu(false);

        Form hi2 = new Form("Hi2 World", BoxLayout.y());
        hi2.add(new Label("Press to show form2"));
        
        hi2.add(new Button(new Command("show form2") {

            @Override
            public void actionPerformed(ActionEvent evt) {
                try {
                    Form hi = new Form("form2");

                    Toolbar tb = hi.getToolbar();
                    Image icon = theme.getImage("icon.png");
                    Container topBar = BorderLayout.east(new Label(icon));
                    topBar.add(BorderLayout.SOUTH, new Label("Cool App Tagline...", "SidemenuTagline"));
                    topBar.setUIID("SideCommand");
                    tb.addComponentToSideMenu(topBar);

                    tb.addMaterialCommandToSideMenu("Home", FontImage.MATERIAL_HOME, e -> {
                    });

                    hi.addComponent(new Label("form2 with sidemenu"));
                    //hi.forceRevalidate();
                    hi.show();
                } catch (Exception ex) {
                    Dialog.show("Error", ex.getMessage(), "OK", null);
                    ex.printStackTrace();
                }
            }
        }));

        hi2.show();
    }

    public void stop() {
        current = getCurrentForm();
        if (current instanceof Dialog) {
            ((Dialog) current).dispose();
            current = getCurrentForm();
        }
    }

    public void destroy() {
    }

}
